(function (vc) {

    vc.extends({
        data: {
            addShareReadingInfo: {
                readingId: '',
                fsmId: '',
                preDegrees: '',
                curDegrees: '',
                preReadingTime: '',
                curReadingTime: '',
                remark: '',
                floorShares:[]
            }
        },
        _initMethod: function () {

        },
        _initEvent: function () {
            vc.on('addShareReading', 'openAddShareReadingModal', function () {
                $that._listAddFloorShares();
                $('#addShareReadingModel').modal('show');
                setTimeout(function(){
                    vc.initDate('preReadingTime', function (_value) {
                        $that.addShareReadingInfo.preReadingTime = _value;
                    });
                    vc.initDate('curReadingTime', function (_value) {
                        $that.addShareReadingInfo.curReadingTime = _value;
                    });
                },1000)
            });
        },
        methods: {
            addShareReadingValidate() {
                return vc.validate.validate({
                    addShareReadingInfo: $that.addShareReadingInfo
                }, {
                    'addShareReadingInfo.fsmId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "公摊表不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "公摊表不能超过30"
                        },
                    ],
                    'addShareReadingInfo.preDegrees': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "上期度数不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "上期度数不能超过30"
                        },
                    ],
                    'addShareReadingInfo.curDegrees': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "本期度数不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "本期度数不能超过30"
                        },
                    ],
                    'addShareReadingInfo.preReadingTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "上期读表时间不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "上期读表时间不能超过30"
                        },
                    ],
                    'addShareReadingInfo.curReadingTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "本期读表时间不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "本期读表时间不能超过30"
                        },
                    ],
                    'addShareReadingInfo.remark': [
  
                        {
                            limit: "maxLength",
                            param: "200",
                            errInfo: "备注不能超过200"
                        },
                    ],




                });
            },
            saveShareReadingInfo: function () {
                if (!$that.addShareReadingValidate()) {
                    vc.toast(vc.validate.errInfo);

                    return;
                }

                $that.addShareReadingInfo.communityId = vc.getCurrentCommunity().communityId;

                vc.http.apiPost(
                    '/meter.saveFloorShareReading',
                    JSON.stringify($that.addShareReadingInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addShareReadingModel').modal('hide');
                            $that.clearAddShareReadingInfo();
                            vc.emit('shareReading', 'listShareReading', {});

                            return;
                        }
                        vc.toast(_json.msg);

                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.toast(errInfo);

                    });
            },
            clearAddShareReadingInfo: function () {
                $that.addShareReadingInfo = {
                    fsmId: '',
                    preDegrees: '',
                    curDegrees: '',
                    preReadingTime: '',
                    curReadingTime: '',
                    remark: '',
                    floorShares:[]
                };
            },
            _listAddFloorShares: function () {
                let param = {
                    params: {
                        page:1,
                        row:300,
                        communityId:vc.getCurrentCommunity().communityId
                    }
                };

                //发送get请求
                vc.http.apiGet('/meter.listFloorShareMeter',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.addShareReadingInfo.floorShares = _json.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _changeShareMeter:function(){
                console.log($that.addShareReadingInfo.fsmId)
                let _fsmId = $that.addShareReadingInfo.fsmId;
                if(!_fsmId){
                    return;
                }
                let _floorShares = $that.addShareReadingInfo.floorShares;
                _floorShares.forEach(s => {
                    if(s.fsmId == _fsmId){
                        $that.addShareReadingInfo.preDegrees = s.curDegree;
                        $that.addShareReadingInfo.preReadingTime = s.curReadingTime;
                    }
                });
            }
        }
    });

})(window.vc);
